package com.cgnpc.scp.purchase.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cgnpc.cud.shiro.util.ContextHolder;
import com.cgnpc.cuddemo.workbench.application.IPermissonApplication;
import com.cgnpc.framework.userinfo.CurrentUser;
import com.cgnpc.scp.common.bean.MyPageResult;
import com.cgnpc.scp.common.pojo.UserInfo;
import com.cgnpc.scp.common.utils.MyPageResultUtil;
import com.cgnpc.scp.common.utils.StringUtil;
import com.cgnpc.scp.purchase.mybatis.entity.ViewCLoseDemand;
import com.cgnpc.scp.purchase.mybatis.entity.ViewDemandChange;
import com.cgnpc.scp.purchase.mybatis.entity.ViewDemandQuery;
import com.cgnpc.scp.purchase.mybatis.entity.ViewPurchaseDemand;
import com.cgnpc.scp.purchase.mybatis.mapper.client.ViewCloseDemandMapper;
import com.cgnpc.scp.purchase.mybatis.mapper.client.ViewDemandChangeMapper;
import com.cgnpc.scp.purchase.mybatis.mapper.client.ViewDemandQueryMapper;
import com.cgnpc.scp.purchase.mybatis.mapper.client.ViewPurchaseDemandMapper;
import com.cgnpc.scp.purchase.service.IDemandQueryService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Set;

/******************************
 * 用途说明: 年度需求
 * 作者姓名: chenshijie
 * 创建时间: 2020/09/23 22:54
 ******************************/
@Service
public class DemandQueryServiceImpl implements IDemandQueryService {

    private static final Logger LOGGER = LoggerFactory.getLogger(DemandQueryServiceImpl.class);

    @Autowired
    private ViewDemandQueryMapper viewDemandQueryMapper;

    @Autowired
    private ViewDemandChangeMapper viewDemandChangeMapper;

    @Autowired
    private ViewPurchaseDemandMapper viewPurchaseDemandMapper;

    @Autowired
    private ViewCloseDemandMapper viewCloseDemandMapper;

    @Autowired
    private IPermissonApplication iPermissonApplication;

    @Autowired
    CurrentUser currentUser;

    /**********************************
     * 用途说明: 年度需求查询
     * 参数说明 params
     * 返回值说明:
     ***********************************/
    @Override
    public MyPageResult<ViewDemandQuery> contractQuery(ViewDemandQuery viewDemandQuery) throws Exception {

        // begin 分页开始
        // 第几页
        int pageNum = viewDemandQuery.getPageNum();
        // 每页的数量
        int pageSize = viewDemandQuery.getPageSize();
        // 创建Page对象，将pageNum，pageSize参数传入，必须位于数据库查询数据的语句之前，否则不生效
//        Page<ViewDemandQuery> queryPage = PageHelper.startPage(pageNum, pageSize);

        // mybatis查询方式
//        ViewDemandQueryExample viewDemandQueryExample = new ViewDemandQueryExample();
//        ViewDemandQueryExample.Criteria criteria = viewDemandQueryExample.createCriteria();

        Page<ViewDemandQuery> page = new Page<>(pageNum, pageSize);

        QueryWrapper<ViewDemandQuery> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("is_change",1);

        // 设置排序字段
//        queryExample.setOrderByClause("lang desc");

        // 设置查询条件
        if (!StringUtil.isNullOrSpace(viewDemandQuery.getYear())) {
//            criteria.andYearLike(SqlUtil.valueJoinLikeBoth(viewDemandQuery.getYear()));
            queryWrapper.like("year",viewDemandQuery.getYear());
        }
        if (!StringUtil.isNullOrSpace(viewDemandQuery.getProject())) {
//            criteria.andProjectLike(SqlUtil.valueJoinLikeBoth(viewDemandQuery.getProject()));
            queryWrapper.like("project",viewDemandQuery.getProject());
        }
        if (!StringUtil.isNullOrSpace(viewDemandQuery.getServiceName())) {
//            criteria.andServiceNameLike(SqlUtil.valueJoinLikeBoth(viewDemandQuery.getServiceName()));
            queryWrapper.like("service_name",viewDemandQuery.getServiceName());
        }
        if (!StringUtil.isNullOrSpace(viewDemandQuery.getContactUser())) {
//            criteria.andContactUserLike(SqlUtil.valueJoinLikeBoth(viewDemandQuery.getContactUser()));
            queryWrapper.like("contact_user",viewDemandQuery.getContactUser());
        }
        if (!StringUtil.isNullOrSpace(viewDemandQuery.getMobile())) {
//            criteria.andMobileLike(SqlUtil.valueJoinLikeBoth(viewDemandQuery.getMobile()));
            queryWrapper.like("mobile",viewDemandQuery.getMobile());
        }
        if (null != viewDemandQuery.getRequiredArrivalTimeStart() && null != viewDemandQuery.getRequiredArrivalTimeEnd()) {
//            criteria.andRequiredArrivalTimeBetween(viewDemandQuery.getRequiredArrivalTimeStart(),viewDemandQuery.getRequiredArrivalTimeEnd());
            queryWrapper.ge("required_arrival_time",viewDemandQuery.getRequiredArrivalTimeStart()).le("required_arrival_time",viewDemandQuery.getRequiredArrivalTimeEnd());
        }

        //管理员查所有  流程创建人只能查询自己的需求
        boolean puradmin = iPermissonApplication.hasRoles("puradmin");
        if(!puradmin) {
            UserInfo userinfo = currentUser.getCurrentUserDetail();
            queryWrapper.and(wq->wq.or(w1->w1.eq("contact_user",userinfo.getUserName()).or(w2->w2.eq("process_user",userinfo.getUserName()))));

//            queryWrapper.or(wq->wq.eq("contact_user",userinfo.getUserName()));
//            queryWrapper.eq("contact_user",userinfo.getUserName());
        }


        // 执行查询
//        List<ViewDemandQuery> viewDemandQueries = viewDemandQueryMapper.selectByExample(viewDemandQueryExample);
        IPage<ViewDemandQuery> processInfoIPage = viewDemandQueryMapper.selectPage(page, queryWrapper);

        // pagehelper分页插件的问题，需要创建PageInfo才能获取总记录数，queryPage.getResult()和dbDataDictList是一样的
//        PageInfo<ViewDemandQuery> pageResult = new PageInfo<>(queryPage.getResult());
        // long pageResultTotal = pageResult.getTotal(); // 返回总数
        // List<DataDict> pageResultList = pageResult.getList(); // 返回当前的列表,pageInfoDataList=dbDataDictList
        // end 分页开始

        // 创建分页结果集对象
//        MyPageResult<ViewDemandQuery> myPageResult = MyPageResultUtil.createMyPageResult(pageResult);
        MyPageResult<ViewDemandQuery> myPageResult = MyPageResultUtil.createMyPageResultPlus(processInfoIPage);
        return myPageResult;
    }

    /**********************************
     * 用途说明: 年度需求变更查询
     * 参数说明 params
     * 返回值说明:
     ***********************************/
    @Override
    public MyPageResult<ViewDemandChange> changeQuery(ViewDemandChange viewDemandChange) throws Exception {
        // begin 分页开始
        // 第几页
        int pageNum = viewDemandChange.getPageNum();
        // 每页的数量
        int pageSize = viewDemandChange.getPageSize();
        // 创建Page对象，将pageNum，pageSize参数传入，必须位于数据库查询数据的语句之前，否则不生效
//        Page<ViewDemandChange> queryPage = PageHelper.startPage(pageNum, pageSize);
        Page<ViewDemandChange> page = new Page<>(pageNum, pageSize);

        QueryWrapper<ViewDemandChange> queryWrapper = new QueryWrapper<>();

        // mybatis查询方式
//        ViewDemandChangeExample viewDemandChangeExample = new ViewDemandChangeExample();
//        ViewDemandChangeExample.Criteria criteria = viewDemandChangeExample.createCriteria();

        // 设置排序字段
//        queryExample.setOrderByClause("lang desc");

        // 设置查询条件
        if (!StringUtil.isNullOrSpace(viewDemandChange.getProject())) {
//            criteria.andProjectLike(SqlUtil.valueJoinLikeBoth(viewDemandChange.getProject()));
            queryWrapper.like("project",viewDemandChange.getProject());
        }
        if (!StringUtil.isNullOrSpace(viewDemandChange.getServiceName())) {
//            criteria.andServiceNameLike(SqlUtil.valueJoinLikeBoth(viewDemandChange.getServiceName()));
            queryWrapper.like("service_name",viewDemandChange.getServiceName());
        }
        if (null != viewDemandChange.getRequiredArrivalTimeStart() && null != viewDemandChange.getRequiredArrivalTimeEnd()) {
//            criteria.andRequiredArrivalTimeBetween(viewDemandChange.getRequiredArrivalTimeStart(),viewDemandChange.getRequiredArrivalTimeEnd());
            queryWrapper.ge("arrival_time",viewDemandChange.getRequiredArrivalTimeStart()).le("arrival_time",viewDemandChange.getRequiredArrivalTimeEnd());
        }

        //管理员查所有  流程创建人只能查询自己的需求
        boolean puradmin = iPermissonApplication.hasRoles("puradmin");
        if(!puradmin) {
            UserInfo userinfo = currentUser.getCurrentUserDetail();
            queryWrapper.and(wq->wq.or(w1->w1.eq("contact_user",userinfo.getUserName()).or(w2->w2.eq("create_user_name",userinfo.getUserName()))));

//            queryWrapper.or(wq->wq.eq("contact_user",userinfo.getUserName()));
//            queryWrapper.eq("contact_user",userinfo.getUserName());
        }

        // 执行查询
//        List<ViewDemandChange> viewDemandChanges = viewDemandChangeMapper.selectByExample(viewDemandChangeExample);
        IPage<ViewDemandChange> viewDemandChanges = viewDemandChangeMapper.selectPage(page, queryWrapper);

        // pagehelper分页插件的问题，需要创建PageInfo才能获取总记录数，queryPage.getResult()和dbDataDictList是一样的
//        PageInfo<ViewDemandChange> pageResult = new PageInfo<>(queryPage.getResult());
        // long pageResultTotal = pageResult.getTotal(); // 返回总数
        // List<DataDict> pageResultList = pageResult.getList(); // 返回当前的列表,pageInfoDataList=dbDataDictList
        // end 分页开始

        // 创建分页结果集对象
//        MyPageResult<ViewDemandChange> myPageResult = MyPageResultUtil.createMyPageResult(pageResult);
        MyPageResult<ViewDemandChange> myPageResult = MyPageResultUtil.createMyPageResultPlus(viewDemandChanges);
        return myPageResult;
    }

    /**********************************
     * 用途说明: 为变更流程提供需求查询
     * 参数说明 params
     * 返回值说明:
     ***********************************/
    @Override
    public MyPageResult<ViewPurchaseDemand> queryPurchaseDemand(ViewPurchaseDemand viewPurchaseDemand) throws Exception {
        // begin 分页开始
        // 第几页
        int pageNum = viewPurchaseDemand.getPageNum();
        // 每页的数量
        int pageSize = viewPurchaseDemand.getPageSize();
        // 创建Page对象，将pageNum，pageSize参数传入，必须位于数据库查询数据的语句之前，否则不生效
//        Page<ViewDemandChange> queryPage = PageHelper.startPage(pageNum, pageSize);
        Page<ViewPurchaseDemand> page = new Page<>(pageNum, pageSize);

        QueryWrapper<ViewPurchaseDemand> queryWrapper = new QueryWrapper<>();

        // mybatis查询方式
//        ViewDemandChangeExample viewDemandChangeExample = new ViewDemandChangeExample();
//        ViewDemandChangeExample.Criteria criteria = viewDemandChangeExample.createCriteria();

        // 设置排序字段
//        queryExample.setOrderByClause("lang desc");

        // 执行查询
//        List<ViewDemandChange> viewDemandChanges = viewDemandChangeMapper.selectByExample(viewDemandChangeExample);
        IPage<ViewPurchaseDemand> viewDemandChanges = viewPurchaseDemandMapper.selectPage(page, queryWrapper);

        // pagehelper分页插件的问题，需要创建PageInfo才能获取总记录数，queryPage.getResult()和dbDataDictList是一样的
//        PageInfo<ViewDemandChange> pageResult = new PageInfo<>(queryPage.getResult());
        // long pageResultTotal = pageResult.getTotal(); // 返回总数
        // List<DataDict> pageResultList = pageResult.getList(); // 返回当前的列表,pageInfoDataList=dbDataDictList
        // end 分页开始

        // 创建分页结果集对象
//        MyPageResult<ViewDemandChange> myPageResult = MyPageResultUtil.createMyPageResult(pageResult);
        MyPageResult<ViewPurchaseDemand> myPageResult = MyPageResultUtil.createMyPageResultPlus(viewDemandChanges);
        return myPageResult;
    }

    @Override
    public MyPageResult<ViewCLoseDemand> queryCloseDemand(ViewCLoseDemand viewCLoseDemand) throws Exception {
        // begin 分页开始
        // 第几页
        int pageNum = viewCLoseDemand.getPageNum();
        // 每页的数量
        int pageSize = viewCLoseDemand.getPageSize();
        // 创建Page对象，将pageNum，pageSize参数传入，必须位于数据库查询数据的语句之前，否则不生效
//        Page<ViewDemandChange> queryPage = PageHelper.startPage(pageNum, pageSize);
        Page<ViewCLoseDemand> page = new Page<>(pageNum, pageSize);

        QueryWrapper<ViewCLoseDemand> queryWrapper = new QueryWrapper<>();
        queryWrapper.in("demand_type",1,2);

        // mybatis查询方式
//        ViewDemandChangeExample viewDemandChangeExample = new ViewDemandChangeExample();
//        ViewDemandChangeExample.Criteria criteria = viewDemandChangeExample.createCriteria();

        // 设置排序字段
//        queryExample.setOrderByClause("lang desc");

        // 执行查询
//        List<ViewDemandChange> viewDemandChanges = viewDemandChangeMapper.selectByExample(viewDemandChangeExample);
        IPage<ViewCLoseDemand> viewCLoseDemandIPage = viewCloseDemandMapper.selectPage(page, queryWrapper);

        // pagehelper分页插件的问题，需要创建PageInfo才能获取总记录数，queryPage.getResult()和dbDataDictList是一样的
//        PageInfo<ViewDemandChange> pageResult = new PageInfo<>(queryPage.getResult());
        // long pageResultTotal = pageResult.getTotal(); // 返回总数
        // List<DataDict> pageResultList = pageResult.getList(); // 返回当前的列表,pageInfoDataList=dbDataDictList
        // end 分页开始

        // 创建分页结果集对象
//        MyPageResult<ViewDemandChange> myPageResult = MyPageResultUtil.createMyPageResult(pageResult);
        MyPageResult<ViewCLoseDemand> myPageResult = MyPageResultUtil.createMyPageResultPlus(viewCLoseDemandIPage);
        return myPageResult;
    }

}
